<template>
	<view class="container">
		<l-painter isRenderImage :board="base" @success="createImg" />
		<view class="btns">
			<button class="btn" type="default" @click="toBack">返回</button>
			<button class="btn" type="primary" @click="saveImage">保存</button>
		</view>		
	</view>
</template>
<script>
	import lPainter from '@/uni_modules/lime-painter/components/lime-painter/'
	import {
		apiUrl
	} from '@/api/base.js'
	export default {
		components: {
			lPainter
		},
		data() {
			return {
				fenxiaoCodePath: "",
				imgPath: "",
				product: "",
				base: "",
			}
		},
		onLoad(opt) {

			var that = this
			//获取产品信息
			uni.request({
				url: apiUrl + "addons/car/product/detail",
				data: {
					id: opt.product_id
				},
				method: "GET",
				success: (res) => {
					let data = res.data.data
					that.product = data
					that.getFenxiaoCode(that.product)	
				},
				fail: (res) => {
					console.log(res)
				}

			})

		},
		methods: {
			createImg(res) {
				this.imgPath = res
			},
			getFenxiaoCode(product) {
				uni.request({
					url: apiUrl + "addons/car/fenxiao/getUnlimited",
					data: {
						fxid: uni.getStorageSync("userInfo").id,
						product_id: product.id,
						page: "pages/product/detail"
					},
					method: "GET",
					success: (res) => {
						this.fenxiaoCodePath = apiUrl + res.data.data
						this.base = {
							width: '750rpx',
							height: '1450rpx',
							background: '#FFF',
							views: [
								{
									type: 'image',
									src: this.product.poster,
									mode: 'widthFix',
									css: {
										left: '20rpx',
										top: '20rpx',
										width: '710rpx',
										height: '1420rpx',										
									}
								},
								
								{
									type: 'image',
									src: this.fenxiaoCodePath,
									mode: 'widthFix',
									css: {
										left: '30rpx',
										top: '30rpx',
										width: '120rpx',
										height: '120rpx'
									}
								}
							]
						}
					},
					fail: (res) => {
						console.log(res)
					}
				})
			},
			saveImage() {
				uni.saveImageToPhotosAlbum({
					filePath: this.imgPath,
					success(res) {
						uni.showToast({
							title: '已保存到相册',
							icon: 'success',
							duration: 2000,
							success: function(res) {
								uni.navigateBack({
									delta: 1
								});
							}
						})
					}
				})
			},
			toBack() {
				uni.navigateBack({
					delta: 1
				});
			}
		}
	}
</script>

<style lang="scss">
	
	page{
		background-color: #FFFFFF;
	}
	.container {
		image {
			width: 100vw;
		}
	}
	.btns{
		margin-top: 40rpx;
		margin-bottom: 20rpx;
		display: flex;
		flex-direction: row;
		flex-wrap: wrap;
	}
	.btn{
		border: solid 2rpx #ED0A0A;
		border-radius: 50rpx;
		width: 300rpx;
	}
</style>
